﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

namespace Steve
{
    public abstract class AuditRepositoryBase
    {
        [Import]
        public IAccountService Account { get; set; }


        protected virtual DateTime CurrentRevisionDate
        {
            get
            {
                return DateTime.Now;
            }
        }

        protected virtual Guid? CurrentAuthenticatedMembershipId
        {
            get
            {
                var user = Account.GetAuthenticatedUser();

                if (user == null) return null;

                return (Guid)user.ProviderUserKey;
            }
        }

        protected virtual void ProcessAudit(AuditObject rev)
        {
            if (rev == null) throw new ArgumentNullException("rev");

            rev.RevisionMembershipId = CurrentAuthenticatedMembershipId;
            rev.RevisionDate = CurrentRevisionDate;

            if (rev.IsNew)
            {
                rev.CreateDate = rev.RevisionDate;
                rev.CreateMembershipId = rev.RevisionMembershipId;
            }
        }
    }
}
